package com.jzo2o.market.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.market.enums.ActivityStatusEnum;
import com.jzo2o.market.mapper.ActivityMapper;
import com.jzo2o.market.model.domain.Activity;
import com.jzo2o.market.model.dto.request.ActivityQueryForPageReqDTO;
import com.jzo2o.market.model.dto.request.ActivitySaveReqDTO;
import com.jzo2o.market.model.dto.response.ActivityInfoResDTO;
import com.jzo2o.market.service.IActivityService;
import com.jzo2o.market.service.ICouponService;
import com.jzo2o.market.service.ICouponWriteOffService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-16
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements IActivityService {
    private static final int MILLION = 1000000;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private ICouponService couponService;

    @Resource
    private ICouponWriteOffService couponWriteOffService;

    /**
     * 保存优惠券活动
     * @param activitySaveReqDTO
     */
    @Override
    public void saveActivity(ActivitySaveReqDTO activitySaveReqDTO) {
        Activity activity = this.getById(activitySaveReqDTO.getId());
        Activity bean = BeanUtils.toBean(activitySaveReqDTO, Activity.class);
        if (ObjectUtils.isNotEmpty(activity)){
            bean.setUpdateTime(LocalDateTime.now());
            bean.setUpdateBy(UserContext.currentUserId());
            LambdaUpdateWrapper<Activity> eq = Wrappers.<Activity>lambdaUpdate().eq(Activity::getId, activitySaveReqDTO.getId());
            boolean update = this.update(bean, eq);
            if (!update){
                throw new CommonException("操作失败");
            }
        }else {
            bean.setStatus(ActivityStatusEnum.NO_DISTRIBUTE.getStatus());
            if (ObjectUtils.isEmpty(bean)){
                throw new CommonException("活动不存在");
            }
            boolean save = this.save(bean);
            if (!save){
                throw new DBException("操作失败");
            }
        }
    }

    /**
     * 分页查询优惠券活动
     * @param dto
     * @return
     */
    @Override
    public PageResult<ActivityInfoResDTO> getActivityList(ActivityQueryForPageReqDTO dto) {
        IPage page=new Page();
        page.setCurrent(dto.getPageNo());
        page.setSize(dto.getPageSize());
        LambdaQueryWrapper<Activity> activityLambdaQueryWrapper = Wrappers.<Activity>lambdaQuery()
                .like(ObjectUtil.isNotEmpty(dto.getName()), Activity::getName, dto.getName())
                .like(ObjectUtil.isNotEmpty(dto.getId()), Activity::getId, dto.getId())
                .eq(ObjectUtil.isNotEmpty(dto.getStatus()), Activity::getStatus, dto.getStatus())
                .eq(ObjectUtil.isNotEmpty(dto.getType()), Activity::getType, dto.getType())
                .orderByAsc(ObjectUtil.isNotEmpty(dto.getIsAsc1()), Activity::getId)
                .orderByAsc(ObjectUtil.isNotEmpty(dto.getIsAsc2()), Activity::getId);
        IPage page1 = this.page(page, activityLambdaQueryWrapper);
        List records = page1.getRecords();
        if (CollUtils.isEmpty(records)){
            throw new CommonException("集合为空");
        }
        PageResult<ActivityInfoResDTO> objectPageResult = new PageResult<>();
        objectPageResult.setList(records);
        objectPageResult.setPages(page1.getPages());
        objectPageResult.setTotal(page1.getTotal());
        return objectPageResult;
    }

    /**
     * 查询优惠券活动详情
     * @param id
     * @return
     */
    @Override
    public ActivityInfoResDTO searchActivityById(Long id) {
        Activity activity = this.getById(id);
        if (ObjectUtils.isEmpty(activity)){
            throw new CommonException("活动不存在");
        }
        ActivityInfoResDTO bean = BeanUtils.toBean(activity, ActivityInfoResDTO.class);
        if (ObjectUtils.isEmpty(bean)){
            throw new CommonException("活动不存在");
        }
        return bean;
    }

    /**
     * 撤销活动接口
     * @param id
     */
    @Override
    public void revoke(Long id) {
        Activity activity = this.getById(id);
        if (ObjectUtils.isEmpty(activity)){
            throw new CommonException("活动不存在");
        }
        boolean update = this.lambdaUpdate().eq(Activity::getId, id)
                .set(Activity::getStatus, ActivityStatusEnum.VOIDED.getStatus())
                .update();
        if (!update){
            throw new DBException("操作失败");
        }
    }
}
